<?php

require_once "./google-api-php-client/src/Google_Client.php";
require_once "./google-api-php-client/src/contrib/Google_DriveService.php";
require_once "./google-api-php-client/src/contrib/Google_Oauth2Service.php";


/* Service Account Client */
$SERVICE_ACCOUNT_CLIENT_ID = '822260332168-krehinm0c39jf9fbp2kdnfn4177v9tf0.apps.googleusercontent.com';
$SERVICE_ACCOUNT_EMAIL = '822260332168-krehinm0c39jf9fbp2kdnfn4177v9tf0@developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = './515713ceb88b0c43f7b33cad28010f1a5b68789b-privatekey.p12';

/* Web App Client */
$WEB_CLIENT_ID = '822260332168-l4bdhdita95juhi15vgkfriine73be0v.apps.googleusercontent.com';
$WEB_CLIENT_SECRET = 'vVAqv7dB7GtgQJ7lnoF5ziHI';
$WEB_CLIENT_REDIRECT_URIS = array(
	'http://dev.sva.net/drive/userinfo.php',
	'http://dev.sva.net/drive/listfiles.php',
);

/* Scopes */
$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';	
$WEB_CLIENT_SCOPES = array(
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/userinfo.email',
   'https://www.googleapis.com/auth/userinfo.profile'
);

/* Drive functions */
function createPublicFolder($service, $folderName, $parentId) {
  $file = new Google_DriveFile();
  $file->setTitle($folderName);
  $file->setMimeType('application/vnd.google-apps.folder');
  if ($parentId != null) {
    $parent = new Google_ParentReference();
    $parent->setId($parentId);
    $file->setParents(array($parent));
  }
  $createdFile = $service->files->insert($file, array(
      'mimeType' => 'application/vnd.google-apps.folder',
  ));

  $permission = new Google_Permission();
  $permission->setValue('');
  $permission->setType('anyone');
  $permission->setRole('reader');

 $service->permissions->insert(
      $createdFile->getId(), $permission);

  return $file;
}

function retrieveAllFiles($service) {
  $result = array();
  $pageToken = NULL;

  do {
    try {
      $parameters = array();
      if ($pageToken) {
        $parameters['pageToken'] = $pageToken;
      }
      $files = $service->files->listFiles($parameters);

      $result = array_merge($result, $files->getItems());
      $pageToken = $files->getNextPageToken();
    } catch (Exception $e) {
      print "An error occurred: " . $e->getMessage();
      $pageToken = NULL;
    }
  } while ($pageToken);
  return $result;
}

?>
